﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace QLBH.StandardUI
{    
    public partial class ProgressDlg : Form
    {        
        public enum eProgressType
        {
            PT_CREATEDB,
            PT_DEL_ODL_DB
        };
        private Thread thread;        
        public bool IsAutoRun = true;
        public ProgressDlg()
        {
            InitializeComponent();

            thread = new Thread(DoSmt);
        }
        public ProgressDlg(bool isAutoRun)
        {
            InitializeComponent();

            thread = new Thread(DoSmt);

            IsAutoRun = isAutoRun;            
        }
        public void SetError()
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new SetCompletedProgrerssDeledate(SetError));
            }
            else
            {
                label1.ForeColor = Color.Red;
                label1.Text = "Lỗi";
                button1.Enabled = true;
                thread.Abort();
                thread = null;
            }
        }
        /// <summary>
        /// Set completed status for Progress
        /// </summary>
        public void SetCompletedProgress()
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new SetCompletedProgrerssDeledate(SetCompletedProgress));
            }
            else
            {
                label1.ForeColor = Color.Blue;
                label1.Text = "Thành Công";
                progressBar1.Value = 100;
                button1.Enabled = true;
                thread.Abort();
                thread = null;
                this.Close();                
            }
        }
        private delegate void SetCompletedProgrerssDeledate();

        private void ProgressDlg_Load(object sender, EventArgs e)
        {
            if (IsAutoRun)
            {
                thread.Start();
            }
            
        }
        bool _stop = false;
        void DoSmt()
        {
            while (!_stop)
            {
                BeginInvoke(new ChangeProgressDelegate(ChangeProgress));
                Thread.Sleep(200);
            }
        }
        private delegate void ChangeProgressDelegate();
        /// <summary>
        /// Change progress value
        /// </summary>
        private void ChangeProgress()
        {
            if (progressBar1.Value < 90)
            {
                progressBar1.Value += 5;
                label1.Text = "Đang tạo CSDL : " + progressBar1.Value.ToString() + "%";
            }
            else
            {
                _stop = true;
            }
        }  
      
        public void POST(int value,eProgressType progressType)
        {
            PostEvent(value, progressType);
        }
        private delegate void PostEventDelegate(int value,eProgressType proType);

        private void PostEvent(int value, eProgressType proType)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new PostEventDelegate(PostEvent), new object[] { value, proType });
            }
            else
            {
                progressBar1.Minimum = 0;
                progressBar1.Maximum = 100;
                progressBar1.Value = value;
                label1.Text = String.Format(GetProgressStatusMessage(proType), value);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (thread != null)
            {
                thread.Abort();
                thread = null;
            }

            this.Close();
        }
        private String GetProgressStatusMessage(eProgressType proType)
        {
            String result = "";
            switch (proType)
             {
                 case eProgressType.PT_CREATEDB:
                     result= "Đang tạo CSDL : {0}%";                     
                     break;
                 case eProgressType.PT_DEL_ODL_DB:
                     result = "Đang xóa CSDL cũ";
                     break;
             }
             return result;
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
